package com.wl.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.exceptions.DeserializationException;
import org.apache.hadoop.hbase.security.visibility.CellVisibility;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.NavigableMap;

public class MyHbaseClient {

    private Configuration conf = null;
    private Connection con = null;
    private Admin admin = null;
    @Before
    public void init() throws IOException {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux121,linux122");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        con = ConnectionFactory.createConnection(conf);
        admin = con.getAdmin();
    }

    @After
    public void distory() throws IOException {
        if(admin != null){
            admin.close();
        }

        if(con != null){
            con.close();
        }
    }

    @Test
    public void createTable() throws IOException {

        HTableDescriptor techer = new HTableDescriptor(TableName.valueOf("teacher"));
        techer.addFamily(new HColumnDescriptor("info"));
        admin.createTable(techer);
        System.out.println("teacher 表创建成功！！！");
    }

    @Test
    public void putData() throws IOException {

        Table teacher = con.getTable(TableName.valueOf("app_user"));
        Put put = new Put(Bytes.toBytes("uuid1"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uuid2"),Bytes.toBytes("uuid2"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uuid3"),Bytes.toBytes("uuid3"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uuid4"),Bytes.toBytes("uuid4"));

        teacher.put(put);
        teacher.close();
        System.out.println("向 teacher 表插入数据成功！！！");
    }

    @Test
    public void putData1() throws IOException {

        Table teacher = con.getTable(TableName.valueOf("app_user"));
        Put put = new Put(Bytes.toBytes("uuid2"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uuid1"),Bytes.toBytes("uuid1"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uuid3"),Bytes.toBytes("uuid3"));
        teacher.put(put);
        teacher.close();
        System.out.println("向 teacher 表插入数据成功！！！");
    }

    @Test
    public void putData2() throws IOException {

        Table teacher = con.getTable(TableName.valueOf("app_user"));
        Put put = new Put(Bytes.toBytes("uuid3"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uuid1"),Bytes.toBytes("uuid1"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uuid2"),Bytes.toBytes("uuid2"));
        teacher.put(put);
        teacher.close();
        System.out.println("向 teacher 表插入数据成功！！！");
    }

    @Test
    public void putData3() throws IOException {

        Table teacher = con.getTable(TableName.valueOf("app_user"));
        Put put = new Put(Bytes.toBytes("uuid4"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uuid1"),Bytes.toBytes("uuid1"));
        teacher.put(put);
        teacher.close();
        System.out.println("向 teacher 表插入数据成功！！！");
    }

    @Test
    public void deleteData() throws IOException {
        Table teacher = con.getTable(TableName.valueOf("app_user"));

        Delete delete = new Delete(Bytes.toBytes("uuid1"));
        delete.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uuid4"));
        teacher.delete(delete);
        teacher.close();
        System.out.println("数据删除成功！！！");

    }

    @Test
    public void getDataByCF() throws IOException {

        Table teacher = con.getTable(TableName.valueOf("teacher"));

        Get get = new Get(Bytes.toBytes("110"));
        get.addFamily(Bytes.toBytes("info"));
        Result result = teacher.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            String family = Bytes.toString(CellUtil.cloneFamily(cell));
            String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
            String value = Bytes.toString(CellUtil.cloneValue(cell));
            String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
            System.out.println("rowkey ---> " + rowkey + "family ---> " + family + "qualifier ---> " + qualifier +
                    "value ---> " + value);
        }

        teacher.close();
        System.out.println("按rowkey查找数据成功！！！");

    }

    @Test
    public void getDataScan() throws IOException {

        Table teacher = con.getTable(TableName.valueOf("teacher"));

        Scan scan = new Scan();

        ResultScanner results = teacher.getScanner(scan);
        for (Result result : results) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                System.out.println("rowkey ---> " + rowkey + "  family ---> " + family + "  qualifier ---> " +
                        qualifier + "  value ---> " + value);
            }
        }

        teacher.close();
        System.out.println("全表扫描成功！！！");

    }

    @Test
    public void scanRowKey() throws IOException {

        Table teacher = con.getTable(TableName.valueOf("teacher"));

        Scan scan = new Scan();
        scan.setStartRow(Bytes.toBytes("001"));
        scan.setStopRow(Bytes.toBytes("2"));
        ResultScanner results = teacher.getScanner(scan);
        for (Result result : results) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                System.out.println("rowkey ---> " + rowkey + "  family ---> " + family + "  qualifier ---> " +
                        qualifier + "  value ---> " + value);
            }
        }

        teacher.close();
        System.out.println("scan 按序列查找成功！！！");

    }
}
